﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace eduscrum.ViewModels
{
    public class SprintVM : IValidatableObject
    {
        public int SprintID { get; set; }

        /*[Required]
        public int Number { get; set; }*/

        [Required]
        [DisplayName("Start date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime StartDate { get; set; }

        [Required]
        [DisplayName("End date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime EndDate { get; set; }

        public virtual List<UserStoryVM> SprintBacklog { get; set; }
        public ProjectVM Project { get; set; }

        public DateTime ProjectStartDate { get; set; }
        public DateTime ProjectEndDate { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var StartDateField = new[] { "StartDate" };
            var EndDateField = new[] { "EndDate" };

            if (StartDate < ProjectStartDate)
            {
                yield return new ValidationResult("The sprint start date cannot be set before the project start date", StartDateField);
            }

            if (EndDate > ProjectEndDate)
            {
                yield return new ValidationResult("The sprint end date cannot be set after the project end date", EndDateField);
            }

            if (EndDate < StartDate)
            {
                yield return new ValidationResult("The sprint end date cannot be set before or on the sprint start date", EndDateField);
            }
        }
    }
}